package com.jade.editor.object.editors.parts;

import java.io.File;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;

public class SettingsImage extends Dialog {

	private Point _size;
	private int _dist;
	private Image _img;
	private boolean _pressed = false;
	private int _mx;
	private int _my;
	private Point _imgLoc = new Point(0, 0);
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingsImage(Shell parentShell, Point size, File file, int dist, Point startLoc) {
		super(parentShell);
		_size = size;
		_dist = dist;
		_img = new Image(parentShell.getDisplay(), file.getPath());
		_imgLoc = startLoc;
	}
	
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Image Settings");
		
	}
	
	@Override
	protected boolean isResizable()  {
	  return true;
	}
	
	public Point getImageLocation(){
		return _imgLoc;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(container, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		scrolledComposite.setMinSize(300, 300);
	
		
		final Composite drawImit = new Composite(scrolledComposite, SWT.NONE);
		drawImit.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
			
				int countX = _size.x / _dist;
				int countY = _size.y / _dist;
				
				e.gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
			
				for(int i = 0, x = 0; i <= countX; i++, x += _dist){
					e.gc.drawLine(x, 0, x, _size.y);
				}
				
				for(int i = 0, y = 0; i <= countY; i++, y += _dist){
					e.gc.drawLine(0, y, _size.x, y);
				}
			}
		});
		drawImit.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		drawImit.setSize(_size);
		drawImit.setLayout(null);
		
		final Composite imgLayer = new Composite(drawImit, SWT.NONE);
		imgLayer.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if(_pressed){
					int x = imgLayer.getLocation().x + (e.x - _mx);
					int y = imgLayer.getLocation().y + (e.y - _my);
					imgLayer.setLocation(x, y);
				}
			}
		});
		
		imgLayer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				_pressed = _pressed ? false : true;
				_mx = e.x;
				_my = e.y;
			}
			@Override
			public void mouseUp(MouseEvent e) {
				_pressed = _pressed ? false : true;
//				imgLayer.redraw();
				_imgLoc = imgLayer.getLocation();
			}
		});
		
		imgLayer.setBounds(_imgLoc.x, _imgLoc.y, _img.getImageData().width, _img.getImageData().height);
		imgLayer.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				
				e.gc.drawImage(_img, 0, 0);
				
//				int countX = imgLayer.getSize().x / _dist;
//				int countY = imgLayer.getSize().y / _dist;
//				
//				int nullLocX = 20 - imgLayer.getLocation().x % _dist;
//				int nullLocY = 20 - imgLayer.getLocation().y % _dist;
//				
//				e.gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
//				
//				for(int i = 0, x = nullLocX; i <= countX; i++, x += _dist){
//					e.gc.drawLine(x, 0, x, drawImit.getSize().y);
//				}
//				
//				for(int i = 0, y = nullLocY; i <= countY; i++, y += _dist){
//					e.gc.drawLine(0, y, drawImit.getSize().x, y);
//				}
				
				
			}
		});
		
		imgLayer.setLayout(null);
		scrolledComposite.setContent(drawImit);
		
		_imgLoc = imgLayer.getLocation();

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(695, 579);
	}
}
